import { wxRequest } from '../../../utils/server.js';
const _wxRequest = new wxRequest();
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    deviceList: [],
    imgUrl: app.globalData.imgUrl,
    uploadUrl: wx.getStorageSync('uploadUrl'),
    storeItem: {},
    supplyItem: {}, //供应商
    orgType: wx.getStorageSync('userInfo').orgType
  },


  onLoad: function (options) {
    this.getlist();
    let orgType = wx.getStorageSync('userInfo').orgType
    //this.getSupplierList()
  },

  // 获取供应商列表
  getSupplierList: function () {
    var that = this
    let url = '/org/protected/bizOrg/findAgentProxySupList'
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + url,
      method: 'POST',
      data: {}
    }, (res) => {
      var data = res.data
      console.log(res);
      if (data && data.length == 1) {
        that.setData({
          supplyItem: {
            id: data[0].id,
            supName: data[0].supName
          }
        })
      }
      console.log(this.data.supplyItem);
    })
  },

  // 获取店铺列表
  getlist: function () {
    var that = this
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + "/org/protected/bizOrg/findStoreList",
      method: "post",
      data: {
        keyword: that.data.keywords,
        status: 1,
        pageSize: 10000
      }
    }, function (res) {
      if (res.list && res.list.length > 0) {
        that.setData({
          storeItem: res.list[0]
        })
      }

    })
  },

  onShow: function () {
    this.getDeviceList()
  },

  handleChoose: function (e) {
    var params = e.currentTarget.dataset
    wx.navigateTo({
      url: '/pages/publish/template/commonlist?apiurl=' + params.apiurl + '&name=' + params.name + '&itemName=' + params.itemname + '&addUrl=' + params.addurl
    })
  },

  handleStore: function () {
    wx.navigateTo({
      url: '/pages/publish/template/store',
    })
  },



  scanQrCode(e) {
    wx.scanCode({
      onlyFromCamera: true,
      scanType: ["barCode", "qrCode"],
      success: (res) => {        if (res.errMsg == "scanCode:ok") {
          let barCode = res.result;
          if (res.result.indexOf('a?g1=') != -1) {
            barCode = res.result.split('?g1=')[1]
          }
          if (res.result.indexOf('a?b1=') != -1) {
            barCode = res.result.split('?b1=')[1].slice(0, 12)
          }
          this.setData({
            ["cententDetail.code"]: barCode
          })
        }
      }
    })
  },

  scanQrCodeSto(e) {
    wx.scanCode({
      onlyFromCamera: true,
      scanType: ["barCode", "qrCode"],
      success: (res) => {
        if (res.errMsg == "scanCode:ok") {
          let barCode = res.result;
          console.log('扫描二维码', barCode)
          this.getStoreVO(barCode)
        }
      }
    })
  },
  getStoreVO(barCode) {
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + "/org/protected/comm/beanCrud/org.StoreVO/getById/" + barCode,
      method: "get",
    }, (res) => {
      console.log(res)
      this.setData({
        storeItem: res.vo
      })
    })
  },


  //查询列表
  getDeviceList: function () {
    var params = {
      keyword: this.data.keyword,
      pageNo: this.data.pageNo,
      pageSize: 30
    }

    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/gds/protected/device/queryTypeList',
      method: 'POST',
      data: params
    }, (res) => {
      let list = res.list;

      this.setData({
        deviceList: list,
        // pageNo: res.page.pageNo,
        // hasNextPage: res.page.hasNextPage,
      });
    })
  },

  bindPickerChange: function (e) {
    var val = e.detail.value
    this.setData({
      typeIndex: val
    })
  },

  //表单提交
  formSubmit: function (e) {
    var formdata = e.detail.value;

    //formdata.supId = this.data.supplyItem.id;
    //formdata.supName = this.data.supplyItem.supName;
    //if (formdata.supName == '') {
    //  wx.showToast({
    //    icon: 'none',
    //    title: '供应商名称不能为空',
    //  })
    //  return
    //}

    // if (!this.data.typeIndex) {
    //   wx.showToast({
    //     icon: 'none',
    //     title: '选择绑定设备',
    //   })
    //   return
    // }   


    if (!this.data.storeItem.stoName) {
      wx.showToast({
        icon: 'none',
        title: '请选择绑定门店',
      })
      return
    }

    if (formdata.code == '' || !formdata.code) {
      wx.showToast({
        icon: 'none',
        title: '请填写设备编码',
      })
      return
    }

    formdata.stoId = this.data.storeItem.id
    //formdata.typeId=this.data.deviceList[this.data.typeIndex].id

    console.log(formdata, 'formdata')
    this.formRequest(formdata);
  },




  //请求
  formRequest: function (formdata) {
    var url = '/gds/protected/device/bind';
    var title = "绑定成功"
    //修改
    // if (this.data.id) {
    //   url = "/gds/protected/comm/beanCrud/iot.DeviceTypeVO/update";
    //   formdata.id = this.data.cententDetail.id;
    //   title = "修改绑定成功"
    // }  

    if (formdata.note == '' || (!formdata.note)) {
      wx.showToast({
        title: '请填写设备标签',
        icon: 'none'
      })
      return;
    }

    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + url,
      method: 'POST',
      data: formdata
    }, (res) => {
      console.log(res)
      wx.showToast({
        title,
      })
      setTimeout(() => {
        // wx.switchTab({  
        //   url: '/pages/publish/bind_device/list',
        // })
        wx.navigateBack({
          delta: 0,
        })
      }, 1500)

    })
  },





})